package com.ltdd.minichat;

import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;

import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class LoginActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);	
	    
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		//getMenuInflater().inflate(R.menu.activity_login, menu);
		return true;
	}
	//TODO: Đăng ký.
	public void doRegister(View view){
		
	}
	
	//TODO: Đăng Nhập
	public void doLogin(View view){
		String id = ((EditText)findViewById(R.id.txtId)).getText().toString();
		String pass = ((EditText)findViewById(R.id.txtPass)).getText().toString();
			System.out.println(id.length());
			if(id.length() == 0 ){
			    Toast.makeText(this, "Xin nhập đúng tên tài khoản", Toast.LENGTH_SHORT).show();
			    ((EditText)findViewById(R.id.txtId)).requestFocus();
		    }else if(pass.length() ==0 ){
			    Toast.makeText(this, "Xin nhập password", Toast.LENGTH_SHORT).show();
			    ((EditText)findViewById(R.id.txtPass)).requestFocus();			    
		    }
		    else{
				final ProgressDialog myPd_ring=ProgressDialog.show(LoginActivity.this, "Vui lòng đợi", "Đang kết nối..", true);
			    myPd_ring.setCancelable(true);
			    new Thread(new Runnable() {  
			    	private Socket sock;
			    	
			          @Override			          
			          public void run() {
			               
			                	String serverip = getResources().getString(R.string.serverip);
			                	int serverport = Integer.parseInt(getResources().getString(R.string.serverport));
			                	System.out.println(serverip);
			               try
			                {   InetAddress inAdress = InetAddress.getByName(serverip) ;
			                    sock = new Socket(inAdress,serverport);
			                    OutputStream o = sock.getOutputStream();
			                    PrintWriter output = new PrintWriter(o);
			                    output.println("Hello Android!");
			                }catch(Exception e){
			                	System.out.println(e.getMessage());
			                }
			                myPd_ring.dismiss();
			          }
			    }).start();
		    }
		
	}
	
	
	

}
